//
//  WebImageView.m
//  3.28 练习
//
//  Created by Justsoso on 16/3/28.
//  Copyright © 2016年 Justsoso. All rights reserved.
//

#import "WebImageView.h"

@implementation WebImageView


- (instancetype)initWithURL:(NSURL *)url
{
    self = [super init];
    if (self) {
//        NSURLRequest *request = [NSURLRequest requestWithURL:url];
//        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
//            if (connectionError == nil)
//            {
//                self.image = [UIImage imageWithData:data];
//            }
//        }];
        mData = [NSMutableData data];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        [connection start];

    }
    return self;
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [mData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    self.image = [UIImage imageWithData:mData];
}

@end
